package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminMPSDao;
import ex.com.jm.food.entity.MenuProfitSummary;

/**
 * AdminMPSDao实现层
 * @author zhaojw
 *
 */
public class AdminMPSDaoImpl extends HibernateDaoSupport implements AdminMPSDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<MenuProfitSummary> getMPSs(final int initPage,final int pageRow,
			final String name, final String s_date,final String e_date,final String sort,final String order) {
		// TODO Auto-generated method stub
		Object mpss=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from MenuProfitSummary mps where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql += " and mps.menuName like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql +=" and mps.sDate >='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " and mps.sDate <= '"+e_date+"'";
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql +=" order by mps."+order+" "+sort;
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<MenuProfitSummary>) mpss;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int mpsCount(final String name,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from MenuProfitSummary mps where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql += " and mps.menuName like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql +=" and mps.sDate >='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " and mps.sDate <= '"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<MenuProfitSummary> MPSExcel(final String name,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		Object mpss=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from MenuProfitSummary mps where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql += " and mps.menuName like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql +=" and mps.sDate >='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " and mps.sDate <= '"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<MenuProfitSummary>) mpss;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<MenuProfitSummary> MPSReport(final String name,final String s_date,
			final String e_date) {
		Object mpss=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from MenuProfitSummary mps where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql += " and mps.menuName like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql +=" and mps.sDate >='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " and mps.sDate <= '"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<MenuProfitSummary>) mpss;
	}

}
